package org.hxyjs.dao.impl;

import org.hxyjs.dao.BookmakerDao;
import org.hxyjs.dto.DTO;
import org.hxyjs.entity.Bookmaker;
import org.hxyjs.entity.Bookmaker;
import org.hxyjs.mapper.BookmakerMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("bookmakerDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class BookmakerDaoImpl implements BookmakerDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private BookmakerMapper bookmakerMapper;

    public List<Bookmaker> getAllBookmakers() throws Exception {
        return bookmakerMapper.selectByExample(null);
    }

    @Override
    public List<Bookmaker> getBookmakerByPage(PageInfo pageInfo) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", pageInfo.getCurrPage());
        map.put("pageSize", pageInfo.getPageSize());
        int itemCount = (int) bookmakerMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return bookmakerMapper.getBookmakerByPage(map);
    }

//    @Override
//    public Bookmaker selectBookmakerByBno(String sno) throws Exception {
//        return bookmakerMapper.selectByPrimaryKey(sno);
//    }


    @Override
    public Integer deleteBookmakerByBno(String bno) {
        return bookmakerMapper.deleteByPrimaryKey(bno);
    }

    @Override
    public Integer updateBookmakerByBno(Bookmaker bookmaker) {
        return bookmakerMapper.updateByPrimaryKey(bookmaker);
    }

    @Override
    public Integer addBookmaker(Bookmaker bookmaker) {
        bookmaker.setBno(initId());
        return bookmakerMapper.insertSelective(bookmaker);
    }
//
//    @Override
//    public Bookmaker selectBookmakerWithorderAndUser(String bno) throws Exception {
//        return bookmakerMapper.selectBookmakerWithorderAndUser(bno);
//    }

    public String initId() {
        String maxBno = bookmakerMapper.getMaxBno();
        return GenNumTools.initId("b", 4, maxBno);
    }

    @Override
    public Bookmaker getBookmakerByBno(String bno) throws Exception {
        return bookmakerMapper.selectByPrimaryKey(bno);
    }

}